//兼容IE8的空console对象
window.console = window.console || {
    log: $.noop
}
/**
 * 支持跨域访问
 * IE8 跨域问题需要注意：点击IE浏览器的的“工具->Internet 选项->安全->自定义级别”将“其他”选项中的“通过域访问数据源”选中为“启用”或者“提示”
 * 如果项目为互联网项目（用户群体不确定，，前端机器不可配置）的情况下，IE8不支持跨域
 *
 */
//全局变量
var GVAR = {};
//后台服务器地址，需配置
GVAR.server = 'http://localhost:8080';
//服务端项目名称
GVAR.ctx = "/hello";

var fileUrl = window.location.href;

var tmp;

;//封装AJAX插件
(function ($, window, document, undefined) {
    //定义AjaxParam的构造函数
    var AjaxParam = function (ele, opt) {
        this.$element = ele,
            this.defaults = {
                type: 'POST',
                dataType: "jsonp",
                contentType: "application/json;charset=utf-8",
                data: "{}",
                success: null,
                error: function (msg) {
                    //添加异常处理框
                    alert(msg);
                },
                async: true
            },
            this.options = $.extend({}, this.defaults, opt)
    }
    //定义AjaxParam的方法
    jQuery.support.cors = true;//IE8跨域问题。
    AjaxParam.prototype = {
        doAjax: function () {
            var dataAjax = {};
            dataAjax.fw_service = this.options.fw_service == null ? "" : this.options.fw_service;
            dataAjax.fw_method = this.options.fw_method == null ? "" : this.options.fw_method;
            dataAjax.fw_mapperid = this.options.fw_mapperid == null ? "" : this.options.fw_mapperid;
            dataAjax.fw_param = this.options.fw_param == null ? "" : this.options.fw_param;
            this.options.data = JSON.stringify(dataAjax);
            this.options.url = encodeURI(encodeURI(this.options.url));
            this.options.url += "?fw_service=" + dataAjax.fw_service + "&fw_method=" + dataAjax.fw_method+"&t="+Math.random();
            this.options.xhrFields = {withCredentials: true };  //  不支持IE8；
            this.options.crossDomain = true;
            var funcError = this.options.error;
            var funcSuccess = this.options.success;
            this.options.success = function (data) {
                if (data.code == "-99") {   //未登录.
                    //应该统一处理：执行跳转
                    return false;
                }
                if (data.code == "-1") {   //后台报错
                    //程序出错
                    funcError(data);
                }
                if (data.code > 0) {   //正常情况
                    //程序出错
                    funcSuccess(data);
                }
            };
            this.options.error = function (xhr, exception) {
                alert("接口调用出错:" + xhr.status+","+JSON.stringify(exception));
            }
            console.log(this.options);
            return $.ajax(this.options);
        }
    }
//在插件中使用AjaxParam对象
    $.fwDoService = function (options) {
        //创建AjaxParam的实体
        options.url = GVAR.server + GVAR.ctx + "/fw/doService.do";
        var ajaxParam = new AjaxParam(this, options);
        //调用其方法
        return ajaxParam.doAjax();
    }
})
(jQuery, window, document);

/**
 * 将前端UI日期 转为数据库日期
 * @param d 日期@格式为：YYYY年MM月DD日 HH24:MM: SS,YYYY-MM-DD HH24:MM: SS,YYYY-MM-DD,YYYY年MM月DD日等
 * @constructor
 */
function FwGetDBdate(d) {
    if (d == undefined) {
        return "";
    }
    var date = "";
    var array = d.split(" ")[0].split(/-|年|月|日|\.|\//);
    date = array[0] + (parseInt(array[1]) < 10 && array[1].length == 1 ? "0" + array[1] : array[1]) + (parseInt(array[2]) < 10 && array[2].length == 1 ? "0" + array[2] : array[2]);
    date += d.split(" ")[1] == undefined ? "" : d.split(" ")[1].replace(/:/g, "");
    return date
}
/**
 * 将数据库格式转为前端日期
 * 原格式 YYYYMMDDHH24MMSS 转 YYYY-MM-DD...
 * @param d
 * @returns {string}
 * @constructor
 */
function FwGetUIdate(d) {
    if (d == undefined) {
        return "";
    }
    var y = new Array();
    y[0] = d.substr(0, 4);
    y[1] = d.substr(4, 2);
    if (d.length == 6) {
        return  y[0] + "-" + y[1];
    }
    y[2] = d.substr(6, 2);   //小时
    if (d.length == 8) {
        return  y[0] + "-" + y[1] + "-" + y[2];
    }
    y[3] = d.substr(8, 2);   //小时
    y[4] = d.substr(10, 2);  //分
    y[5] = d.substr(12, 2);  //秒
    var rs = y[0] + '-' + y[1] + '-' + y[2];
    if (y[3] != '') {
        rs += ' ' + y[3] + ':' + y[4] + ':' + y[5];
    }
    return rs;
}

/**
 * 获取当前时间，返回格式：2017-03-29 11:41:10
 * @returns {string}
 */
function FwGetNow() {
    var date = new Date();
    var sign1 = "-";
    var sign2 = ":";
    var year = date.getFullYear() // 年
    var month = date.getMonth() + 1; // 月
    var day = date.getDate(); // 日
    var hour = date.getHours(); // 时
    var minutes = date.getMinutes(); // 分
    var seconds = date.getSeconds() //秒
    // 给一位数数据前面加 “0”
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (day >= 0 && day <= 9) {
        day = "0" + day;
    }
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    var currentdate = year + sign1 + month + sign1 + day + " " + hour + sign2 + minutes + sign2 + seconds;
    return currentdate;
}

var expires = 30;//COOKIE 有效期30天
/**
 * 页面COOKIE操作
 * @type {{readJSON: readJSON, setJSON: setJSON, delJSON: delJSON, getJSONS: getJSONS, clearCookie: clearCookie}}
 */
var cookies = {
    readJSON: function (n) {//名字  读cookies
        var dc = "; " + document.cookie + "; ";
        var coo = dc.indexOf("; " + n + "=");
        if (coo != -1) {
            var s = dc.substring(coo + n.length + 3, dc.length);
            return  JSON.parse(unescape(s.substring(0, s.indexOf("; "))));
        } else {
            return null;
        }
    },
    setJSON: function (name, json) {//名字 值 过期时间  写cookies
        var expDays = expires * 24 * 60 * 60 * 1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + expDays);
        var expString = expires ? "; expires=" + expDate.toGMTString() : "";
        var pathString = ";path=/";
        document.cookie = name + "=" + JSON.stringify(json) + expString + pathString;
    },
    delJSON: function (name) {//名字 删除cookies
        var value = ""
        expires = "0"
        var expDays = expires * 24 * 60 * 60 * 1000;
        var expDate = new Date();
        expDate.setTime(expDate.getTime() + expDays);
        var expString = expires ? "; expires=" + expDate.toGMTString() : "";
        var pathString = ";path=/";
        document.cookie = name + "=" + value + expString + pathString;
    },
    getJSONS: function () {
        var arr = new Array();//创建一个数组
        var dc = document.cookie;
        var info = dc.split(";");
        for (var idx = 0; idx < info.length; idx++) {
            var json = JSON.parse(info[idx].split("=")[1]);
            arr.push(json);// 将一个或多个新元素添加到数组结尾，并返回数组新长度
        }
        return arr;
    },
    clearCookie: function () {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if (keys) {
            for (var i = keys.length; i--;)
                this.delJSON(keys[i]);
        }
    }
};